/*
 * @Description: 项目启动
 * @Author: maskMan
 * @LastEditTime: 2025-03-17 12:32:35
 */
/**初始化启动服务 */
import login from './routes/login'
import api from './routes/api'
import user from './routes/user'
import admin from './routes/admin'
import Mondb from './mongo/mondb'
import { AppListen, AppMiddleWare, getApp, httpListen } from './public/httpServe'
import StorBase from './mongo/storage'
import Joint from './mongo/joint'
import { Table } from './mongo/struct'
import { getWeightData } from './public/gameApp'
import { conf } from './public/config'
import { STATUS } from './types/enums'
async function main() {
  const app = getApp()
  if (app) {
    if (conf.status == STATUS.TEST) {
      conf.resUrl = 'http://192.168.124.5:8080'
    }
    console.log(conf)
    StorBase.init()
    AppListen()
    if (!AppMiddleWare()) {
      console.error(`启动失败-AppMiddleWare`)
      return
    }
    if (!(await httpListen())) {
      console.error(`启动失败-httpsListen`)
      return
    }
    if (!(await Mondb.connect())) {
      console.error(`启动失败-Mondb.connect`)
      return
    }
    if (!(await Mondb.creatSchemas())) {
      console.error(`Mondb.creatSchemas`)
      return
    }
    await Mondb.dropCollection(Table.admin)
    //创建管理员账户
    await Joint.createAdmin()
    await Joint.createWeight()
    await getWeightData()
    app.use('/admin', admin)
    app.use('/login', login)
    app.use('/user', user)
    app.use('/api', api)
  }
}
main()
